package
{
	import Box2D.Common.Math.b2Vec2;
	
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	import phi.engine.actors.PolygonActor;
	import phi.engine.actors.SensorActor;
	import phi.engine.core.Game;
	import phi.engine.core.GameTime;
	import phi.engine.input.KeyboardState;
	import phi.engine.layers.HScrollLayer;
	import phi.engine.layers.ILayer;
	import phi.engine.layers.Layer;
	import phi.engine.layers.ScrollActorLayer2;
	import phi.engine.utils.PhiMathUtil;
	import phi.game.ball.actors.BallActor;
	import phi.game.ball.actors.BuildingActor;
	import phi.game.ball.actors.HillsActor;
	import phi.game.ball.actors.MountainsActor;
	import phi.game.ball.layers.BlocksScrollLayer;
	
	public class RunGame extends Game
	{
		// Protected vars
		protected var textField :TextField;
		protected var ball :BallActor = null;
		
		public function RunGame( window :DisplayObjectContainer )
		{
			super( window );
		}
		
		// --------------------------------------------------------------------
		// Public functions
		// --------------------------------------------------------------------
		
		override public function init():void
		{
			super.init();
			
			createLayers();
			createBallActor();
			createSensors();
			
			// Score
			textField = new TextField();
			textField.width = 100;
			textField.height = 50;
			textField.text = "0";
			
			window.addChild( textField );
		}
		
		override public function update( gameTime:GameTime ):void
		{
			super.update( gameTime );
			updateScore();
		}
		
		override public function handleInput():void
		{
			super.handleInput();
			
			var state :KeyboardState = engine.keyboardState();
			
			if( state.isKeyDown( Keyboard.SPACE ) )
				engine.pause = !engine.pause; 
		}
		
		// --------------------------------------------------------------------
		// Protected functions
		// --------------------------------------------------------------------
		
		protected function createLayers():void
		{
			var hillsLayer :HScrollLayer = new HScrollLayer();
			var mountainsLayer :HScrollLayer = new HScrollLayer();
			var bloksLayer :BlocksScrollLayer = new BlocksScrollLayer();
			
			// Hills
			hillsLayer.scrollActorClass = HillsActor;
			hillsLayer.velocity = new Point( 2, 0 );
			hillsLayer.padding = -14;
			hillsLayer.y = 150;
			
			// Mountains
			mountainsLayer.scrollActorClass = MountainsActor;
			mountainsLayer.velocity = new Point(0.5, 0 );
			mountainsLayer.padding = -5;
			mountainsLayer.y = 80;
			
			// Blocks
			bloksLayer.scrollActorClass = BuildingActor;
			bloksLayer.padding = 5;
			bloksLayer.topPadding = 632;
			bloksLayer.velocity = new Point( 200, 0 );
			bloksLayer.velocityDelta = new Point( 1, 0 );
			
			sceneManager.addLayer( mountainsLayer );
			sceneManager.addLayer( hillsLayer );
			sceneManager.addLayer( bloksLayer );
			sceneManager.addLayer( new Layer(), true );
			
		}
		
		protected function createBallActor():void
		{
			var layer :ILayer = sceneManager.defaultLayer;
			
			ball = new BallActor( new Point(100, 130) );
			layer.addActor( ball );
		}
		
		protected function createSensors():void
		{
			var layer :ILayer = sceneManager.defaultLayer;
			
			layer.addActor( new SensorActor("wall", new Point(-5, engine.screenHeight/2), new Point(10, engine.screenHeight)));
			layer.addActor( new SensorActor("wall", new Point(engine.screenWidth/2, engine.screenHeight + 5), new Point(engine.screenWidth, 10)));
		}
		
		protected function updateScore():void
		{		
			var layer :HScrollLayer = sceneManager.layers[2];
		
			if( layer )
			{
				var pixels :Number = (layer.actors[0].x - layer.actors[0].width/2) * -1;
				textField.text = Math.round( PhiMathUtil.pixelsToMeters( pixels )).toString() + " m";
			}
		}
	}
}